const context = {}
const config = require("../config")
context.config = config

/**
 * 成功返回 使用function的原因是将this指向ctx ctx.success() 点的左边是谁，this就会指向谁
 * @param {object} data 
 * @param {number} status 默认200
 */
context.success = function (data, status = 200) {
    const statusIsNumber = typeof status === 'number'
    if (!statusIsNumber) {
        throw new Error("返回状态码status 必须是number类型")
    }
    this.status = status
    this.body = { msg: 'success', status, data }
}



/**
 * 失败返回
 * @param {object} reason 
 * @param {number} status 默认500
 */
context.fail = function (reason, status = 500) {
    const statusIsNumber = typeof status === 'number'
    if (!statusIsNumber) {
        throw new Error("返回状态码status 必须是number类型")
    }
    this.status = status
    this.body = { msg: 'fail', status, reason }
}

// const model = require("../model")
// context.model = model
const model = require("../model")
context.model = model

module.exports = context